package terra.twl.window;

import org.newdawn.slick.Input;

import terra.Loader;
import terra.network.packet.client.RequestChatMessage;
import terra.slick.GameState;
import de.matthiasmann.twl.Button;
import de.matthiasmann.twl.EditField;
import de.matthiasmann.twl.EditField.Callback;
import de.matthiasmann.twl.ResizableFrame;
import de.matthiasmann.twl.ScrollPane;
import de.matthiasmann.twl.TextArea;
import de.matthiasmann.twl.textarea.HTMLTextAreaModel;

public class ChatPanel extends ResizableFrame {

    private EditField field;
    Button ok;
    private StringBuilder sb;
    private HTMLTextAreaModel textAreaModel;
    private TextArea textArea;
    public  ScrollPane scrollPane;
    
    public ChatPanel(){
    	setTheme("chat-frame");
    	setTitle("Chat");
    	setResizableAxis(ResizableAxis.NONE);
    	sb = new StringBuilder();
        textAreaModel = new HTMLTextAreaModel();
        textArea = new TextArea(textAreaModel);
        textArea.setBorderSize(0, 0);
        scrollPane = new ScrollPane(textArea);
        scrollPane.setFixed(ScrollPane.Fixed.HORIZONTAL);
        field = new EditField();
        field.setTheme("editfield");
        field.setBorderSize(0, 0);
        field.addCallback(new Callback(){
			@Override
			public void callback(int key) {
				if (key == Input.KEY_RETURN){
					if (field.getText().length() != 0)
						Loader.handler.send(new RequestChatMessage(GameState.getInstance().player.getObjectId(),field.getText()));
					field.setText("");
				}
			}});
        ok = new Button("send");
        ok.addCallback(new Runnable(){

			@Override
			public void run() {
				if (field.getText().length() != 0)
					Loader.handler.send(new RequestChatMessage(GameState.getInstance().player.getObjectId(),field.getText()));
				field.setText("");
			}});
        
        ok.setSize(32,16);
        field.setSize(200, 16);
        scrollPane.setSize(250, 100); 
        textArea.setSize(250, 100);
        
        add(scrollPane);
        add(field);
        add(ok);
       // appendRow("default", "Welcome to the server");
    }
    
    @Override
    protected void layout() {
    	scrollPane.setPosition(getInnerX(), getInnerY());
        field.setPosition(getInnerX(),getInnerY()+scrollPane.getHeight()+5);
        ok.setPosition(getInnerX()+getInnerWidth()-ok.getWidth()-5, getInnerY()+scrollPane.getHeight()+5);
        layoutCloseButton();
        //super.layout();
    }
    
    @Override
    public int getPreferredInnerWidth() {
        return 250;
    }

    @Override
    public int getPreferredInnerHeight() {
        return 125;
    }
    
    
    public boolean isAtEnd = false;
    
    public void appendRow(String font, String text) {
    	isAtEnd = false;
        sb.append("<div style=\"word-wrap: break-word; font-family: ").append(font).append("; \">");
        for(int i=0,l=text.length() ; i<l ; i++) {
            char ch = text.charAt(i);
            switch(ch) {
                case '<': sb.append("&lt;"); break;
                case '>': sb.append("&gt;"); break;
                case '&': sb.append("&amp;"); break;
                case '"': sb.append("&quot;"); break;
                case 'h':
                    if(text.startsWith("http://", i)) {
                        int end = i + 7;
                        while(end < l && isURLChar(text.charAt(end))) {
                            end++;
                        }
                        String href = text.substring(i, end);
                        sb.append("<a style=\"font: link\" href=\"").append(href)
                                .append("\" >").append(href)
                                .append("</a>");
                        i = end - 1;
                        break;
                    }
                default:
                    sb.append(ch);
            }
        }
        sb.append("</div>");
        isAtEnd = scrollPane.getMaxScrollPosY() == scrollPane.getScrollPositionY();
        textAreaModel.setHtml(sb.toString());
    }
    
    private boolean isURLChar(char ch) {
        return (ch == '.') || (ch == '/') || (ch == '%') ||
                (ch >= '0' && ch <= '9') ||
                (ch >= 'a' && ch <= 'z') ||
                (ch >= 'A' && ch <= 'Z');
    }
    
}
